<?php

namespace App\Http\Controllers\Api;

use App\Http\Requests\Api\CollectRequest;
use App\Models\Collect;
use App\Transformers\CollectTransformer;
use Illuminate\Http\Request;
use App\Models\Movie;
use App\Models\User;
use App\Transformers\UserTransformer;
use App\Transformers\MovieTransformer;

class CollectsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $user_id = $request->input('user_id', null);
        if ($user_id && !$user = User::find($user_id)) {
            return $this->respones->error('用户不存在', 404);
        } else if(!$user_id) {
            $user = $this->user();
        }
        $collects = Collect::where('user_id', $user->id)->limit(20)->get();
        $userTransformer = new UserTransformer;
        return $this->response->collection($collects, new CollectTransformer)->addMeta('user', $userTransformer->transform($user));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(CollectRequest $request)
    {
        $collect = new Collect;
        $collect->fill($request->all());
        $collect->user_id = $this->user()->id;
        if ($collect->save()) {
            // 创建看单，将电影加入看单
            $movie_ids = $request->input('movie_ids', []);
            if (count($movie_ids) > 0) {
                $collect->movies()->sync($movie_ids);
                foreach($movie_ids as $movie_id) {
                    // 修改属性
                    $this->user()->updateWatchStatus($movie_id, 'inCollect');
                }
            }
            return $this->response->item($collect, new CollectTransformer())->setStatusCode(201);
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show(Collect $collect)
    {
        return $this->response->item($collect, new CollectTransformer());
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Collect $collect, CollectRequest $request)
    {
        // return $this->user();
        // 验证权限
        $this->authorize('update', $collect);
        $collect->fill($request->all());
        if ($movie_ids = $request->input('movie_ids', [])) {
            $collect->movies()->syncWithoutDetaching($movie_ids);
            // 将电影加入看单的同时，加入用户正在刷的列表
            if (count($movie_ids) > 0) {
                foreach($movie_ids as $movie_id) {
                    $this->user()->updateWatchStatus($movie_id, 'inCollect');
                }
            }
        }
        $collect->save();
        return $this->response->item($collect, new CollectTransformer())->setStatusCode(201);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(Collect $collect)
    {
        $this->authorize('delete', $collect);
        $collect->delete();
        return $this->response->noContent()->setStatusCode(204);
    }

    public function removeMovies(Collect $collect, CollectRequest $request)
    {
        $this->authorize('update', $collect);
        $movie_ids = $request->input('movie_ids', []);
        $collect->movies()->detach($movie_ids);
        return $this->response->item($collect, new CollectTransformer());
    }
    public function addMovies(Collect $collect, Request $request)
    {
        $this->authorize('update', $collect);
        $movie_ids = $request->input('movie_ids', []);
        $collect->movies()->syncWithoutDetaching($movie_ids);
        // 将电影加入看单的同时，加入用户正在刷的列表
        if (count($movie_ids) > 0) {
            foreach($movie_ids as $movie_id) {
                $this->user()->updateWatchStatus($movie_id, 'inCollect');
            }
        }
        return $this->response->item($collect, new CollectTransformer());
    }

    public function movies(Collect $collect)
    {
        $movies = $collect->movies()->paginate(10);
        if ($this->user()) {
            $watchs = $this->user()->watchStatusWithMovies($movies->pluck('id'));
        } else {
            $watchs = null;
        }
        return $this->response->paginator($movies, new MovieTransformer($watchs));
    }
}
